package com.renren.rrwx.model;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.renren.mobile.rmsdk.core.RMConnectCenter;
import com.renren.mobile.rmsdk.core.base.UserInfo;
import com.renren.mobile.rmsdk.core.exception.RRException;
import com.renren.mobile.rmsdk.friends.GetFriendsRequest;
import com.renren.mobile.rmsdk.friends.GetFriendsResponse;
import com.renren.mobile.rmsdk.friends.GetFriendsResponse.FriendItem;

/**
 * 用于管理当前全部的英雄的,一个单例
 * 
 * @author Leon
 * 
 */
public class HeroManager {

	public static String TAG = "HeroManager";

	private ArrayList<Hero> heros;

	// 当前的玩家
	private PlayerHero player;

	// 与人人网的连接

	private Context mContext;

	private static HeroManager instance;

	private HeroManager() {
		heros = new ArrayList<Hero>();
	}

	/**
	 * 获得HeroManager的实例，传入当前的Context
	 * 
	 * @param context
	 * @return
	 */
	public static synchronized HeroManager getInstance(Context context) {

		if (instance == null)
			instance = new HeroManager();
		if (context instanceof Activity)
			context = context.getApplicationContext();
		instance.mContext = context;
		return instance;
	}

	/**
	 * 获得英雄总数
	 * 
	 * @return
	 */
	public int getHeroCount() {
		return heros.size();
	}

	/**
	 * 根据Id索引英雄
	 * 
	 * @param id
	 * @return
	 */
	public Hero findHeroById(int id) {
		for (Hero h : heros) {
			if (h.id == id)
				return h;
		}
		return null;
	}

	/**
	 * 根据索引获得英雄
	 * 
	 * @param idx
	 * @return
	 */
	public Hero findHeroByIndex(int idx) {
		if (idx >= 0 && idx < heros.size())
			return heros.get(idx);
		return null;
	}

	public ArrayList<Hero> getHeros() {
		return this.heros;
	}

	/**
	 * 获取玩家的Hero对象
	 * 
	 * @return
	 */
	public PlayerHero getPlayer() {
		return player;
	}

	/**
	 * 读取玩家个人的数据
	 */
	private void loadUser(UserInfo u) {

		player = (PlayerHero) HeroFactory.create(HeroFactory.HERO_PLAYER,
				u.getUserId(), u.getUserName());
		boolean added = false;
		for (int i = 0; i < heros.size(); i++) {
			if (heros.get(i).id > player.id) {
				heros.add(i, player);
				player.setRank(i);
				added = true;
				break;
			}
		}
		if (!added) {
			heros.add(player);
			player.setRank(heros.size());
		}

	}

	/**
	 * 读取全部人人网好友
	 * 
	 * @param friends
	 */
	private void loadFriends(FriendItem[] friends) {
		//
		if (friends == null)
			return;
		LOGD("Load friends! friends=" + friends);
		// int count = friends.length;
		heros = new ArrayList<Hero>();
		int tr = 0;
		for (FriendItem f : friends) {

			Hero newHero = HeroFactory.create(HeroFactory.HERO_FRIEND, f);
			LOGD(newHero.toString());
			newHero.setRank(tr);
			heros.add(newHero);
			tr += 1;
		}
	}

	public static void LOGD(String msg) {
		Log.d(TAG, msg);
	}

	public void initRenrenData() throws RRException {
		RMConnectCenter center = RMConnectCenter.getInstance(mContext);
		// 请求朋友列表
		GetFriendsRequest getFriendsRequest = new GetFriendsRequest.Builder()
				.setPage(1).setPageSize(1).create();
		GetFriendsResponse getFriendsResPonse;
		getFriendsResPonse = center.request(getFriendsRequest);
		// 执行上面两句为了得到好友的数量
		int count = getFriendsResPonse.getCount();
		// 设为一页，一页数量为friendsCount,即可一次取出所有好友
		getFriendsRequest = new GetFriendsRequest.Builder().setPage(1)
				.setPageSize(count).create();
		getFriendsResPonse = center.request(getFriendsRequest);
		FriendItem[] friends = getFriendsResPonse.getFriends();
		this.loadFriends(friends);
		// 请求当前玩家数据
		this.loadUser(center.getUserInfo());
	}
}
